package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.SignAdapter;
import com.jianyun.jyzs.bean.Sign;
import com.jianyun.jyzs.bean.SignDateInfo;
import com.jianyun.jyzs.dao.SignDao;
import com.jianyun.jyzs.utils.DateTime;
import com.jianyun.jyzs.utils.DateUtils;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.calendar.CalendarAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.jianyun.jyzs.utils.RceLog;

/**
 * 签到日历展示界面
 * Created by yangshijun on 2018/5/30.
 */

public class SignCalendarActivity extends AppCompatActivity implements CalendarAdapter.ItemClickListener {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.left_img)
    TextView leftImg;
    @BindView(R.id.btn_prev_month)
    LinearLayout btnPrevMonth;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.right_img)
    TextView rightImg;
    @BindView(R.id.btn_next_month)
    LinearLayout btnNextMonth;
    @BindView(R.id.calendar)
    RecyclerView calendar;
    @BindView(R.id.list)
    ListView listView;

    private List<SignDateInfo> dateList;
    private List<List<Sign>> calendarData = new ArrayList<>();
    private Calendar baseCalendar;
    private CalendarAdapter adapter;
    private SignDao dao;
    private SignAdapter listadapter;
    private String enterpriseCode;
    private String userId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_signin_calendar);
        ButterKnife.bind(this);
        initData();
        initVIew();
    }

    private void initVIew() {
        topBack.setVisibility(View.VISIBLE);
        topText.setText("签到记录");
        baseCalendar = Calendar.getInstance();
        baseCalendar.setTime(new Date());
        dateList = new ArrayList<>();
        initCalendar(baseCalendar);
        calendar.setLayoutManager(new GridLayoutManager(this, 7));
        RceLog.d("lml", dateList.toString());
        adapter = new CalendarAdapter(this, dateList);
        calendar.setAdapter(adapter);
        List<Sign> daoListData = dao.getListByDate(userId, DateTime.getStringDate(new Date().getTime()), enterpriseCode);
        listadapter = new SignAdapter(this, daoListData);
        listView.setAdapter(listadapter);
        adapter.setItemClickListener(this);
    }


    private void initData() {
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        dao = new SignDao(this);
    }

    @OnClick({R.id.topBack, R.id.btn_prev_month, R.id.btn_next_month})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.btn_prev_month:
                baseCalendar.add(Calendar.MONTH, -1);
                initCalendar(baseCalendar);
                adapter.setList(dateList);
                break;
            case R.id.btn_next_month:
                baseCalendar.add(Calendar.MONTH, 1);
                initCalendar(baseCalendar);
                adapter.setList(dateList);
                break;
        }
    }

    @Override
    public void onitemClick(List<Sign> bean) {
        listadapter.setList(bean);
    }

    private void initCalendar(Calendar calendar) {
        dateList.clear();
        calendarData.clear();
        int year = calendar.get(Calendar.YEAR);
        String year_ = year + "";
        int month = calendar.get(Calendar.MONTH) + 1;
        tvMonth.setText(year + "年" + month + "月");//
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        Calendar calendarClone = (Calendar) calendar.clone();
        calendarClone.set(Calendar.DAY_OF_MONTH, 1);
        int startDayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK);
        calendarClone.set(Calendar.DAY_OF_MONTH, maxDay);
        int endDayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK);
        String month_ = (month + "").length() > 1 ? (month + "") : "0" + month;
        String format = year_ + "-" + month_ + "-";
        for (int i = 0; i < maxDay; i++) {
            int d = i + 1;
            String day_ = (d + "").length() > 1 ? (d + "") : "0" + d;
            //在这里搜索数据
            List<Sign> listData = dao.getListByDate(userId, format + day_, enterpriseCode);
            if (listData.size() > 0) {
                RceLog.d("lml", "数据库查到的数据=" + listData.toString());
            }
            calendarData.add(listData);
        }

        int startEmptyCount = startDayOfWeek - 1;
        Calendar preCalendar = (Calendar) calendar.clone();
        preCalendar.set(Calendar.DAY_OF_MONTH, 1);
        preCalendar.add(Calendar.DAY_OF_MONTH, -startEmptyCount);
        for (int i = 0; i < startEmptyCount; i++) {
            SignDateInfo dateInfo = new SignDateInfo(); //使用DateInfo来储存所需的相关信息
            dateInfo.setDate(preCalendar.getTime());
            dateInfo.setDayOfM(preCalendar.get(Calendar.DAY_OF_MONTH));
            dateInfo.setType(SignDateInfo.PRE_MONTH); //标记日期信息的类型为上个月
            dateList.add(dateInfo); //将日期添加到数组中
            preCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String todaystring = DateUtils.YMD(new Date());
        for (int i = 0; i < maxDay; i++) {
            SignDateInfo dateInfo = new SignDateInfo();
            dateInfo.setDate(calendar.getTime());
            dateInfo.setDayOfM(calendar.get(Calendar.DAY_OF_MONTH));
            dateInfo.setType(SignDateInfo.CURRENT_MONTH);  //标记日期信息的类型为当月
            dateInfo.setBean(calendarData.get(i));
            String ymd = DateUtils.YMD(calendar.getTime());
            if (ymd.equals(todaystring)) {
                dateInfo.setToday(true);
            } else {
                dateInfo.setToday(false);
            }
            dateList.add(dateInfo);
            calendar.add(Calendar.DAY_OF_MONTH, 1);

        }
        int endEmptyCount = 7 - endDayOfWeek;
        Calendar afterCalendar = (Calendar) calendar.clone();
        for (int i = 0; i < endEmptyCount; i++) {
            SignDateInfo dateInfo = new SignDateInfo();
            dateInfo.setDate(afterCalendar.getTime());
            dateInfo.setDayOfM(afterCalendar.get(Calendar.DAY_OF_MONTH));
            dateInfo.setType(SignDateInfo.AFTER_MONTH);  //标记日期信息的类型为下个月
            dateList.add(dateInfo);
            afterCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        this.baseCalendar.add(Calendar.MONTH, -1);
    }
}
